package com.huilian.iotbox.admin.wap.controller;

import com.huilian.iotbox.admin.wap.service.CommodityStockRecordService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.CommodityStockRecordDto;
import com.huilian.iotbox.data.enums.RoleEnum;
import com.huilian.iotbox.data.service.CommodityStockRecordCommonService;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 龙 路 飞
 * @CreateDate: 2023-09-09 16:00
 * @Description:
 */
@RestController
@RequestMapping("/api/commodityStockRecord")
public class CommodityStockRecordController extends UserInfoController {
    @Autowired
    private CommodityStockRecordService commodityStockRecordService;
    @Autowired
    private CommodityStockRecordCommonService commodityStockRecordCommonService;
    /**
     * 根据条件查询
     */
    @PostMapping("/findCommodityStockRecordList")
    public ServerResponse findCommodityStockRecordList(@RequestBody CommodityStockRecordDto commodityStockRecordDto, HttpServletRequest request){
        UserInfo userInfo = super.getLoginUserInfo(request);
        commodityStockRecordDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(commodityStockRecordService.findCommodityStockRecordList(commodityStockRecordDto));
    }

    @PostMapping("/insertInventory")
    public ServerResponse insertInventory(@RequestBody CommodityStockRecordDto commodityStockRecordDto, HttpServletRequest request){
        UserInfo userInfo = super.getLoginUserInfo(request);
        commodityStockRecordDto.setUserInfo(userInfo);
        if (RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())) {
            commodityStockRecordDto.setUserId(userInfo.getParentId());
        } else {
            commodityStockRecordDto.setUserId(userInfo.getId());
        }
        commodityStockRecordService.insertInventory(commodityStockRecordDto);
        return ServerResponse.createBySuccess();
    }

    @PostMapping("/findInventoryByUserId")
    public ServerResponse findInventoryByUserId(@RequestBody CommodityStockRecordDto commodityStockRecordDto, HttpServletRequest request){
        UserInfo userInfo = super.getLoginUserInfo(request);
        commodityStockRecordDto.setUserInfo(userInfo);
        if (RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())) {
            commodityStockRecordDto.setUserId(userInfo.getParentId());
        } else {
            commodityStockRecordDto.setUserId(userInfo.getId());
        }
        return ServerResponse.createBySuccess(commodityStockRecordService.findInventoryByUserId(commodityStockRecordDto));
    }
    @PostMapping("/findTakeStockOfOutOfStock")
    public ServerResponse findTakeStockOfOutOfStock(@RequestBody CommodityStockRecordDto commodityStockRecordDto, HttpServletRequest request){
        UserInfo userInfo = super.getLoginUserInfo(request);
        commodityStockRecordDto.setUserInfo(userInfo);
        if (RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())) {
            commodityStockRecordDto.setUserId(userInfo.getParentId());
        } else {
            commodityStockRecordDto.setUserId(userInfo.getId());
        }
        return ServerResponse.createBySuccess(commodityStockRecordService.findTakeStockOfOutOfStock(commodityStockRecordDto));
    }

    @PostMapping("/updateCommodityStockRecordStatus")
    public ServerResponse updateCommodityStockRecordStatus(@RequestBody CommodityStockRecordDto commodityStockRecordDto, HttpServletRequest request){
        UserInfo userInfo = super.getLoginUserInfo(request);
        commodityStockRecordDto.setUserInfo(userInfo);
        if (RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())) {
            commodityStockRecordDto.setUserId(userInfo.getParentId());
        } else {
            commodityStockRecordDto.setUserId(userInfo.getId());
        }
        return ServerResponse.createBySuccess(commodityStockRecordService.updateCommodityStockRecordStatus(commodityStockRecordDto));
    }

    @PostMapping("/updateCommodityStockRecordItemStatus")
    public ServerResponse updateCommodityStockRecordItemStatus(@RequestBody CommodityStockRecordDto commodityStockRecordDto, HttpServletRequest request){
        UserInfo userInfo = super.getLoginUserInfo(request);
        commodityStockRecordDto.setUserInfo(userInfo);
        if (RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())) {
            commodityStockRecordDto.setUserId(userInfo.getParentId());
        } else {
            commodityStockRecordDto.setUserId(userInfo.getId());
        }
        return ServerResponse.createBySuccess(commodityStockRecordService.updateCommodityStockRecordItemStatus(commodityStockRecordDto));
    }
    @PostMapping("/findStockRecord")
    public ServerResponse findStockRecord(@RequestBody CommodityStockRecordDto commodityStockRecordDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        commodityStockRecordDto.setUserInfo(userInfo);
        try {
            return ServerResponse.createBySuccess(commodityStockRecordCommonService.findStockRecord(commodityStockRecordDto));
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByError(e.getMessage());
        }
    }
}
